﻿using Ma.Bussiness.AttributeExtend;
using Ma.Bussiness.ModelExtend;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ma.Bussiness.DBExtend
{
    public static class TSqlHelper<T> where T : BaseModel
    {
        public static string FindSql = null;
        public static string AddSql = null;
        public static string DeleteSql = null;
        public static string EditSql = null;
        private static Type type = typeof(T);
        private static string TableName = AttributeHelper.GetMappingTName<T>();
        static TSqlHelper()
        {
            string columnName = string.Join(",", type.GetProperties().Select(prop => $"[{prop.GetMappingColumnName()}]"));
            FindSql = $"SELECT {columnName} FROM [{TableName}]";

            var props = type.GetProperties().Where(p => !p.Name.Equals("Id"));
            var columns = string.Join(",", props.Select(prop => $"[{prop.GetMappingColumnName()}]=@{prop.GetMappingColumnName()}"));
            EditSql = $"UPDATE [{TableName}] SET {columns}";

            DeleteSql = $"DELETE FROM [{TableName}]";

            columnName = string.Join(",", props.Select(prop => $"[{prop.GetMappingColumnName()}]"));
            string columnValue = string.Join(",", props.Select(prop => $"@{prop.GetMappingColumnName()}"));
            AddSql = $"INSERT INTO [{TableName}]({columnName}) VALUES({columnValue})";
        }
    }
}
